package com.echat.serviceapigateway.security.tool;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by chenmohan on 2018/08/30
 *
 * @description
 */
public class EncryptUtils {

    private static BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    private static AtomicInteger loop = new AtomicInteger(1);

    private static final String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final String AES_SAULT = "echat-services";

    public static String uuid(){
        return UUID.randomUUID().toString().replace("-","").toUpperCase();
    }

    public static String bcrypt(String str){
        return encoder.encode(str);
    }

    /**
     * 生成accessSecret
     * @param str
     * @return
     */
    public static String accessSecret(String str){
        return bcrypt(str).replaceAll("[+/=.]",getRandomCharacter()).substring(0,16);
    }

    /**
     * 生成accessSecret
     * @return
     */
    public static String accessSecret(){
        return bcrypt(UUID.randomUUID().toString()).replaceAll("[+/=.]",getRandomCharacter()).substring(0,16);
    }

    /**
     * 生成一个无意义令牌,令牌中不包含任何信息.
     * @return
     */
    public static String token(){
        String token = (System.currentTimeMillis() + new Random().nextInt(999999999)) + "";
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte md5[] =  md.digest(token.getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(md5).replaceAll("[+/=]",getRandomCharacter());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 生成用户的签名令牌
     * @return
     */
    public static String signToken() {
        String s = bcrypt(UUID.randomUUID().toString()).replaceAll("[+/=.]", getRandomCharacter());
        return s.substring(s.length()-9,s.length()-1);
    }

    /**
     * 生成一个自包含令牌,令牌中包含了一些信息,采用AES对称加密并进行base64编码
     * @param something String 包含的信息
     * @return
     */
    public static String token(String something){
        String token = AESUtil.encrypt(something, AES_SAULT);
        return token;
    }

    /**
     * 对自包含令牌进行解密，返回其中信息
     * @param token 自包含令牌字符串
     * @return
     */
    public static String decrptToken(String token){
        return AESUtil.decrypt(token,AES_SAULT);
    }

    private static String getRandomCharacter(){
       return String.valueOf(chars.charAt( (int)(Math.random() * 52) ));
    }

    public static void main(String[] args) {
        String abc = "123";
        System.out.println( bcrypt(abc) );
        System.out.println( bcrypt(abc) );
        System.err.println( uuid() );
        System.err.println( uuid() );
        System.out.println( token() );
        System.out.println( token() );
    }

}
